Imports atcMwGisUtility
Imports atcData
Imports atcData.atcDataManager

Public Class mwWRDB
    Implements MapWindow.Interfaces.IPlugin

    Private pMapWin As MapWindow.Interfaces.IMapWin

    Private WithEvents WRDB As Interfaces.clsWRDB
    Friend Const REGAPP As String = "WRDB"

    Public ReadOnly Property Author() As String Implements MapWindow.Interfaces.IPlugin.Author
        Get
            Return "Lloyd Chris Wilson, Ph.D., P.E; Wilson Engineering, LLC, St. Louis, Missouri"
        End Get
    End Property

    Public ReadOnly Property BuildDate() As String Implements MapWindow.Interfaces.IPlugin.BuildDate
        Get
            Return System.IO.File.GetLastWriteTime(Me.GetType().Assembly.Location)
        End Get
    End Property

    Public ReadOnly Property Description() As String Implements MapWindow.Interfaces.IPlugin.Description
        Get
            Return "An interface for providing full functionality of the Water Resources Database client."
        End Get
    End Property

    <CLSCompliant(False)> _
    Public Sub Initialize(ByVal MapWin As MapWindow.Interfaces.IMapWin, ByVal ParentHandle As Integer) Implements MapWindow.Interfaces.IPlugin.Initialize
        pMapWin = MapWin
        atcMwGisUtility.GisUtil.MappingObject = MapWin
        AddMenuIfMissing(AnalysisMenuName, "", AnalysisMenuName, "mnuFile")
        AddMenuIfMissing(AnalysisMenuName & "_WRDB", AnalysisMenuName, "Water Resources Database")
        AddButton("Water Resources Database", "WRDB")
    End Sub

    ''' <summary>
    ''' load bitmaps from assembly; for this to work, must add bmp files to project and change properties build action to "Embedded Resource"
    ''' </summary>
    Private Sub AddButton(ByVal ButtonName As String, ByVal BitmapName As String, Optional ByVal NewGroup As Boolean = False)
        Dim _assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
        With pMapWin.Toolbar
            With .AddButton(ButtonName, "", "", "")
                Dim strm As IO.Stream = _assembly.GetManifestResourceStream("WRDB.MapWin." & BitmapName & ".bmp")
                If strm IsNot Nothing Then .Picture = New System.Drawing.Bitmap(strm)
                .Tooltip = ButtonName
                .Description = ButtonName
            End With
        End With
    End Sub

    Public Sub ItemClicked(ByVal ItemName As String, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.ItemClicked
        If ItemName = AnalysisMenuName & "_WRDB" Or ItemName = "Water Resources Database" Then
            GisUtil.MappingObject = pMapWin
            Handled = True
            Dim btn As MapWindow.Interfaces.ToolbarButton = pMapWin.Toolbar.ButtonItem("Water Resources Database")
            Dim mnu As MapWindow.Interfaces.MenuItem = pMapWin.Menus.Item(AnalysisMenuName & "_WRDB")
            Dim PanelName As String = "Water Resources Database"
            If btn.Pressed Then 'make WRDB go away
                WRDB.CloseMainFormMW()
                pMapWin.UIPanel.DeletePanel(PanelName)
                btn.Pressed = False
                mnu.Checked = False
                Application.UseWaitCursor = False
            Else 'instantiate WRDB and setup panel
                Dim DLLPath As String = GetSetting(REGAPP, "Defaults", "DLL Path", "C:\Program Files\Water Resources Database 5.0")
                My.User.InitializeWithWindowsUser()
                If Debugger.IsAttached And My.User.Name.Contains("LCW") Then DLLPath = "C:\Projects\WRDB 5.1\bin"
                Dim DLLName As String = DLLPath & "\WRDBLib.dll"
                If DLLName <> "" AndAlso My.Computer.FileSystem.FileExists(DLLName) Then
                    Dim objHandle As System.Runtime.Remoting.ObjectHandle = System.Activator.CreateInstanceFrom(DLLName, "WRDB.Lib.clsWRDB")
                    WRDB = objHandle.Unwrap
                Else
                    System.Windows.Forms.MessageBox.Show(String.Format("Unable to load WRDB library:\n\n{0}\n\nThis library is automatically registered the first time that the stand-alone WRDB program is run;\nmake sure that WRDB is installed and run it once so that the MapWindow plug-in can function properly.".Replace("\n", vbCr), DLLName), "Warning", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Warning)
                    btn.Pressed = False
                    Exit Sub
                End If
                With WRDB
                    .MapWin = pMapWin
                    Dim frm As Windows.Forms.Form = .GetMainFormMW
                    frm.Visible = False
                    Application.DoEvents()
                    Dim pnl As Windows.Forms.Panel = pMapWin.UIPanel.CreatePanel(PanelName, Windows.Forms.DockStyle.Right)
                    pMapWin.UIPanel.SetPanelVisible(PanelName, False)
                    For i As Integer = frm.Controls.Count - 1 To 0 Step -1
                        pnl.Controls.Add(frm.Controls(i))
                        pnl.Controls(pnl.Controls.Count - 1).BringToFront()
                    Next
                    .ShowMainFormMW()
                    pMapWin.UIPanel.SetPanelVisible(PanelName, True)
                End With
                btn.Pressed = True
                mnu.Checked = True
            End If
        End If
    End Sub

    Public Sub LayerRemoved(ByVal Handle As Integer) Implements MapWindow.Interfaces.IPlugin.LayerRemoved
    End Sub

    <CLSCompliant(False)> _
    Public Sub LayersAdded(ByVal Layers() As MapWindow.Interfaces.Layer) Implements MapWindow.Interfaces.IPlugin.LayersAdded
    End Sub

    Public Sub LayersCleared() Implements MapWindow.Interfaces.IPlugin.LayersCleared
    End Sub

    Public Sub LayerSelected(ByVal Handle As Integer) Implements MapWindow.Interfaces.IPlugin.LayerSelected
    End Sub

    <CLSCompliant(False)> _
    Public Sub LegendDoubleClick(ByVal Handle As Integer, ByVal Location As MapWindow.Interfaces.ClickLocation, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.LegendDoubleClick
    End Sub

    <CLSCompliant(False)> _
    Public Sub LegendMouseDown(ByVal Handle As Integer, ByVal Button As Integer, ByVal Location As MapWindow.Interfaces.ClickLocation, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.LegendMouseDown
    End Sub

    <CLSCompliant(False)> _
    Public Sub LegendMouseUp(ByVal Handle As Integer, ByVal Button As Integer, ByVal Location As MapWindow.Interfaces.ClickLocation, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.LegendMouseUp
    End Sub

    Public Sub MapDragFinished(ByVal Bounds As System.Drawing.Rectangle, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.MapDragFinished
    End Sub

    Public Sub MapExtentsChanged() Implements MapWindow.Interfaces.IPlugin.MapExtentsChanged
        If WRDBActive() Then WRDB.MapExtentsChanged()
    End Sub

    Public Sub MapMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Integer, ByVal y As Integer, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.MapMouseDown
    End Sub

    Public Sub MapMouseMove(ByVal ScreenX As Integer, ByVal ScreenY As Integer, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.MapMouseMove
        If WRDBActive() Then WRDB.MapMouseMove(ScreenX, ScreenY)
    End Sub

    Public Sub MapMouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Integer, ByVal y As Integer, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.MapMouseUp
    End Sub

    Public Sub Message(ByVal msg As String, ByRef Handled As Boolean) Implements MapWindow.Interfaces.IPlugin.Message
    End Sub

    Public ReadOnly Property Name() As String Implements MapWindow.Interfaces.IPlugin.Name
        Get
            Return "Water Resources Database (WRDB)"
        End Get
    End Property

    Public Sub ProjectLoading(ByVal ProjectFile As String, ByVal SettingsString As String) Implements MapWindow.Interfaces.IPlugin.ProjectLoading
    End Sub

    Public Sub ProjectSaving(ByVal ProjectFile As String, ByRef SettingsString As String) Implements MapWindow.Interfaces.IPlugin.ProjectSaving
    End Sub

    Public ReadOnly Property SerialNumber() As String Implements MapWindow.Interfaces.IPlugin.SerialNumber
        Get
            Return "G14R/KCU1FOWVVI"
        End Get
    End Property

    <CLSCompliant(False)> _
    Public Sub ShapesSelected(ByVal Handle As Integer, ByVal SelectInfo As MapWindow.Interfaces.SelectInfo) Implements MapWindow.Interfaces.IPlugin.ShapesSelected
        If WRDBActive() Then WRDB.ShapesSelected()
    End Sub

    Public Sub Terminate() Implements MapWindow.Interfaces.IPlugin.Terminate
        If WRDBActive() Then WRDB.CloseMainFormMW()
        RemoveMenuIfEmpty(AnalysisMenuName & "_WRDB")
        RemoveMenuIfEmpty(AnalysisMenuName)
    End Sub

    Public ReadOnly Property Version() As String Implements MapWindow.Interfaces.IPlugin.Version
        Get
            Return System.Diagnostics.FileVersionInfo.GetVersionInfo(Me.GetType().Assembly.Location).FileVersion
        End Get
    End Property

    Private Function WRDBActive() As Boolean
        Dim btn As MapWindow.Interfaces.ToolbarButton = pMapWin.Toolbar.ButtonItem("Water Resources Database")
        Return btn.Pressed
    End Function

End Class

'the following method is the modern preferred way, but MW continues to nag you about reloading the plugin when done this way; revert to Implements approach above

'Public Class mwWRDB
'    Inherits atcData.atcDataPlugin

'    Private WithEvents WRDB As WRDB.Interfaces.clsWRDB
'    Friend Const REGAPP As String = "WRDB"

'    Public Overrides ReadOnly Property Name() As String
'        Get
'            Return "Water Resources Database (WRDB)"
'        End Get
'    End Property

'    Public Overrides ReadOnly Property Author() As String
'        Get
'            Return "Lloyd Chris Wilson, Ph.D., P.E.; Clayton Engineering Company, St. Louis, Missouri"
'        End Get
'    End Property

'    Public Overrides ReadOnly Property Description() As String
'        Get
'            Return "An interface for providing full functionality of the Water Resources Database client."
'        End Get
'    End Property

'    <CLSCompliant(False)> _
'    Public Overrides Sub Initialize(ByVal aMapWin As MapWindow.Interfaces.IMapWin, _
'                                    ByVal aParentHandle As Integer)
'        MyBase.Initialize(aMapWin, aParentHandle)
'        atcMwGisUtility.GisUtil.MappingObject = aMapWin
'        AddMenuIfMissing(AnalysisMenuName, "", AnalysisMenuName, "mnuFile")
'        AddMenuIfMissing(AnalysisMenuName & "_WRDB", AnalysisMenuName, "Water Resources Database")
'        AddButton("Water Resources Database", "WRDB")
'    End Sub

'    ''' <summary>
'    ''' load bitmaps from assembly; for this to work, must add bmp files to project and change properties build action to "Embedded Resource"
'    ''' </summary>
'    Private Sub AddButton(ByVal ButtonName As String, ByVal BitmapName As String, Optional ByVal NewGroup As Boolean = False)
'        Dim _assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
'        With pMapWin.Toolbar
'            With .AddButton(ButtonName, "", "", "")
'                Dim strm As IO.Stream = _assembly.GetManifestResourceStream("WRDB.MapWin." & BitmapName & ".bmp")
'                If strm IsNot Nothing Then .Picture = New System.Drawing.Bitmap(strm)
'                .Tooltip = ButtonName
'                .Description = ButtonName
'            End With
'        End With
'    End Sub

'    Public Overrides Sub Terminate()
'        If WRDBActive() Then WRDB.CloseMainFormMW()
'        RemoveMenuIfEmpty(AnalysisMenuName & "_WRDB")
'        RemoveMenuIfEmpty(AnalysisMenuName)
'    End Sub

'    Public Overrides Sub ItemClicked(ByVal aItemName As String, ByRef aHandled As Boolean)
'        If aItemName = AnalysisMenuName & "_WRDB" Or aItemName = "Water Resources Database" Then
'            GisUtil.MappingObject = pMapWin
'            aHandled = True
'            Dim btn As MapWindow.Interfaces.ToolbarButton = pMapWin.Toolbar.ButtonItem("Water Resources Database")
'            Dim mnu As MapWindow.Interfaces.MenuItem = pMapWin.Menus.Item(AnalysisMenuName & "_WRDB")
'            Dim PanelName As String = "Water Resources Database"
'            If btn.Pressed Then 'make WRDB go away
'                WRDB.CloseMainFormMW()
'                pMapWin.UIPanel.DeletePanel(PanelName)
'                btn.Pressed = False
'                mnu.Checked = False
'            Else 'instantiate WRDB and setup panel
'                Dim DLLPath As String = GetSetting(REGAPP, "Defaults", "DLL Path", "C:\Program Files\Water Resources Database 5.0")
'                My.User.InitializeWithWindowsUser()
'                If Debugger.IsAttached And My.User.Name.Contains("LCW") Then DLLPath = ""C:\Projects\WRDB 5.1\WRDBDLL\bin"
'                Dim DLLName As String = DLLPath & "\WRDBlib.dll"
'                If DLLName <> "" AndAlso My.Computer.FileSystem.FileExists(DLLName) Then
'                    Dim objHandle As System.Runtime.Remoting.ObjectHandle = System.Activator.CreateInstanceFrom(DLLName, "wrdb.lib.clsWRDB")
'                    WRDB = objHandle.Unwrap
'                Else
'                    System.Windows.Forms.MessageBox.Show(String.Format("Unable to load WRDB library:\n\n{0}\n\nThis library is automatically registered the first time that the stand-alone WRDB program is run;\nmake sure that WRDB is installed and run it once so that the MapWindow plug-in can function properly.".Replace("\n", vbCr), DLLName), "Warning", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Warning)
'                    btn.Pressed = False
'                    Exit Sub
'                End If
'                With WRDB
'                    .MapWin = pMapWin
'                    Dim frm As Windows.Forms.Form = .GetMainFormMW
'                    frm.Visible = False
'                    Application.DoEvents()
'                    Dim pnl As Windows.Forms.Panel = pMapWin.UIPanel.CreatePanel(PanelName, Windows.Forms.DockStyle.Right)
'                    pMapWin.UIPanel.SetPanelVisible(PanelName, False)
'                    For i As Integer = frm.Controls.Count - 1 To 0 Step -1
'                        pnl.Controls.Add(frm.Controls(i))
'                        pnl.Controls(pnl.Controls.Count - 1).BringToFront()
'                    Next
'                    .ShowMainFormMW()
'                    pMapWin.UIPanel.SetPanelVisible(PanelName, True)
'                End With
'                btn.Pressed = True
'                mnu.Checked = True
'            End If
'        End If
'    End Sub

'    Public Overrides Sub MapMouseMove(ByVal aScreenX As Integer, ByVal aScreenY As Integer, ByRef aHandled As Boolean)
'        MyBase.MapMouseMove(aScreenX, aScreenY, aHandled)
'        If WRDBActive() Then WRDB.MapMouseMove(aScreenX, aScreenY)
'    End Sub

'    Public Overrides Sub MapExtentsChanged()
'        MyBase.MapExtentsChanged()
'        If WRDBActive() Then WRDB.MapExtentsChanged()
'    End Sub

'    <CLSCompliant(False)> _
'    Public Overrides Sub ShapesSelected(ByVal aHandle As Integer, ByVal aSelectInfo As MapWindow.Interfaces.SelectInfo)
'        MyBase.ShapesSelected(aHandle, aSelectInfo)
'        If WRDBActive() Then WRDB.ShapesSelected()
'    End Sub

'    Private Function WRDBActive() As Boolean
'        Dim btn As MapWindow.Interfaces.ToolbarButton = pMapWin.Toolbar.ButtonItem("Water Resources Database")
'        Return btn.Pressed
'    End Function
'End Class
